package com.wyl.template.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wyl.template.aop.annotation.SysLogger;
import com.wyl.template.domain.common.PageResult;
import com.wyl.template.domain.common.ResponseResult;
import com.wyl.template.domain.excel.RoleExcel;
import com.wyl.template.domain.excel.UserExcel;
import com.wyl.template.domain.model.SysRoleDO;
import com.wyl.template.domain.model.SysUserDO;
import com.wyl.template.domain.request.RolePageRequest;
import com.wyl.template.domain.request.UserPageRequest;
import com.wyl.template.domain.vo.RolePermissionVO;
import com.wyl.template.domain.vo.UserRoleVO;
import com.wyl.template.enums.LoggerType;
import com.wyl.template.service.SysRoleService;
import com.wyl.template.util.ExcelUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: wyl
 * @Date: 2021/6/22
 * @Description:
 */
@SysLogger(group = "角色")
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController<SysRoleService, SysRoleDO> {

    @Autowired
    private SysRoleService roleService;

    private static final String ROOT_NAME = "root";

    /**
     * 获得所有角色
     *
     * @return
     */
    @SysLogger(name = "查询所有角色", type = LoggerType.SELECT)
    @GetMapping("getRoles")
    public ResponseResult getRoles() {
        Subject subject = SecurityUtils.getSubject();
        SysUserDO user = (SysUserDO) subject.getPrincipal();
        //防止越权，只获得用户所拥有的的角色
        List<SysRoleDO> sysRoleDOS;
        if(ROOT_NAME.equals(user.getLoginName())){
            sysRoleDOS = roleService.list();
        }else{
            sysRoleDOS = roleService.listUserRoles(user.getId());
        }
        return ResponseResult.success(sysRoleDOS);
    }

    /**
     * 分页查询角色
     *
     * @param rolePageRequest
     * @return
     */
    @SysLogger(name = "分页查询角色", type = LoggerType.SELECT)
    @GetMapping("role")
    public ResponseResult getRoles(RolePageRequest rolePageRequest) {
        ResponseResult<IPage<SysRoleDO>> selectPage = this.selectPage(rolePageRequest);
        IPage pageInfo = selectPage.getData();
        List<SysRoleDO> userList = pageInfo.getRecords();
        //查询角色集合关联的权限列表
        List<RolePermissionVO> rolePermissionList = roleService.listRolesPerssionIn(userList);

        PageResult pageResult = new PageResult();
        BeanUtils.copyProperties(pageInfo, pageResult);
        pageResult.setRecords(rolePermissionList);
        return ResponseResult.success(pageResult);
    }

    /**
     * 新增角色
     *
     * @return
     */
    @SysLogger(name = "新增", type = LoggerType.ADD)
    @PostMapping("role")
    public ResponseResult addRole(@RequestBody RolePermissionVO rolePermission) {

        return roleService.addRole(rolePermission);
    }

    /**
     * 修改角色
     *
     * @return
     */
    @SysLogger(name = "修改", type = LoggerType.UPDATE)
    @PutMapping("role")
    public ResponseResult updateRole(@RequestBody RolePermissionVO rolePermission) {

        return roleService.updateRole(rolePermission);
    }

    /**
     * 删除角色
     *
     * @param rolePermission
     * @return
     */
    @SysLogger(name = "删除", type = LoggerType.DELETE)
    @DeleteMapping("role")
    public ResponseResult deleteRole(@RequestBody RolePermissionVO rolePermission) {

        return roleService.deleteRole(rolePermission.getId());
    }

    /**
     * 导出角色信息
     *
     * @param rolePageRequest
     * @param response
     */
    @SysLogger(name = "导出角色信息", type = LoggerType.EXPORT)
    @GetMapping("/download")
    public void download(RolePageRequest rolePageRequest, HttpServletResponse response) {
        ResponseResult<IPage<SysRoleDO>> selectPage = this.selectPage(rolePageRequest);
        IPage pageInfo = selectPage.getData();
        List<SysRoleDO> userList = pageInfo.getRecords();
        //查询角色集合关联的权限列表
        List<RolePermissionVO> rolePermissionList = roleService.listRolesPerssionIn(userList);

        List<RoleExcel> roleExcelList = rolePermissionList.stream().map(rolePermission ->{
            RoleExcel roleExcel = new RoleExcel();
            BeanUtils.copyProperties(rolePermission,roleExcel);
            StringBuilder permissionStr = new StringBuilder(12);
            rolePermission.getPermissionList().forEach(sysPermissionDO -> {
                permissionStr.append(sysPermissionDO.getMenuName());
                permissionStr.append("、");//添加分隔符
            });

            if(permissionStr.length()>1){
                permissionStr.deleteCharAt(permissionStr.length()-1);
            }
            roleExcel.setMenuName(permissionStr.toString());

            return roleExcel;
        }).collect(Collectors.toList());

        ExcelUtil.downloadExcel(response, "角色列表", roleExcelList, RoleExcel.class);
    }

}
